package com.gxkj.projects.myshopx.dao;

import com.gxkj.common.hibernate.HibernateBaseRepositoryImpl;
import com.gxkj.common.utils.ListPager;
import com.gxkj.projects.myshopx.entitys.Goods;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.Map;

@Repository
public class GoodsDaoImpl extends HibernateBaseRepositoryImpl {

    public ListPager<Goods> doPageHQL( int pagenNo, int pageSize,Goods entity){
        ListPager<Goods> pager = new ListPager<Goods>();
        pager.setPageNo(pagenNo);
        pager.setRowsPerPage(pageSize);
        String hql  = " from Goods g  ";
        Map<String,Object> param = new HashMap<String,Object>();
        hql += " where isDelete  = :isDelete ";
        if(entity == null ||   entity.getIsDelete() == 0  ){
            param.put("isDelete", 0);
        }else {
            param.put("isDelete", 1);
        }
        if(entity != null){
            if(StringUtils.isNoneBlank(entity.getCatId())){
                hql += " and catId  = :catId ";
                param.put("catId", entity.getCatId());
            }
            if(StringUtils.isNoneBlank(entity.getBrandId())){
                hql += " and brandId  = :brandId ";
                param.put("brandId", entity.getBrandId());
            }
            if(entity.getIsBest()  != -1 ){
                hql += " and isBest  = :isBest ";
                param.put("isBest", entity.getIsBest());
            }
            if(entity.getIsNew()  != -1 ){
                hql += " and isNew  = :isNew ";
                param.put("isNew", entity.getIsNew());
            }
            if(entity.getIsHot() != -1 ){
                hql += " and isHot  = :isHot ";
                param.put("isHot", entity.getIsHot());
            }
            if(entity.getIsPromote() != -1 ){
                hql += " and isPromote  = :isPromote ";
                param.put("isPromote", entity.getIsPromote());
            }
            if(StringUtils.isNoneBlank(entity.getProviderId())){
                hql += " and providerId  = :providerId ";
                param.put("providerId", entity.getProviderId());
            }

            if(entity.getIsOnSale() != -1 ){
                hql += " and isOnSale  = :isOnSale ";
                param.put("isOnSale", entity.getIsOnSale());
            }
            if(StringUtils.isNoneBlank(entity.getKeywords())){
                hql += " and ( keywords  like :keywords  or goodsName like :keywords or goodsSn like :keywords or id like :keywords )";
                param.put("keywords", "%"+entity.getKeywords()+"%");
            }

        }
        return  this.selectPageByHql(hql,param,pager);
    }
    public boolean isExitByGoodsSn(String id,String goodsSn ){
        String hql = null;
        Map<String,Object> param = new HashMap<String,Object>();
        if( StringUtils.isBlank(id)){
            hql = " from Goods a where isDelete != :isDelete and goodsSn=:goodsSn ";
            param.put("isDelete", 0);
            param.put("goodsSn",goodsSn);
        }else {
            hql = " from Goods a where isDelete != :isDelete and goodsSn=:goodsSn and id != :id";
            param.put("isDelete", 0);
            param.put("goodsSn",goodsSn);
            param.put("id",id);
        }
        Goods entity = this.selectFirstOneByHQL(hql,param);
        if(entity == null){
            return false;
        }else {
            return true;
        }

    }
}
